﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.ClientInfoCommand;
using SmartProduction.Read.API.Dtos;

namespace SmartProduction.Read.API.Application.CommandHandlers.ClientInfoCommandHandler
{
    /// <summary>
    /// 客户信息查询命令处理
    /// </summary>
    public class ClientInfoQueryCommandHandler : IRequestHandler<ClientInfoQueryCommand, APIResult<APIPaing<ClientInfoDTO>>>
    {
        /// <summary>
        /// 客户信息仓储
        /// </summary>
        private readonly IBaseRepository<ClientInfo> clientRepository;

        /// <summary>
        /// 客户行业仓储
        /// </summary>
        private readonly IBaseRepository<Industry> industryRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="clientRepository">客户信息仓储</param>
        /// <param name="industryRepository">客户行业仓储</param>
        /// <param name="mapper">映射</param>
        public ClientInfoQueryCommandHandler(IBaseRepository<ClientInfo> clientRepository,IBaseRepository<Industry> industryRepository,IMapper mapper)
        {
            this.clientRepository = clientRepository;
            this.industryRepository = industryRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<APIPaing<ClientInfoDTO>>> Handle(ClientInfoQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaing<ClientInfoDTO>> result = new APIResult<APIPaing<ClientInfoDTO>>();
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
            var linq = from a in industryRepository.GetAll().Result
                       join b in clientRepository.GetAll().Result on a.IndustryId equals b.IndustryId
                       select new ClientInfoDTO
                       {
                          ClientInfoNo = b.ClientInfoNo,
                          ClientInfoAddr = b.ClientInfoAddr,
                          ClientInfoType = b.ClientInfoType,
                          ClientInfoClass = b.ClientInfoClass,
                          ClientInfoEmail = b.ClientInfoEmail,
                          ClientInfoLabel = b.ClientInfoLabel,
                          ClientInfoName = b.ClientInfoName,
                          ClientInfoNumer = b.ClientInfoNumer,
                          ClientInfoPhone = b.ClientInfoPhone,
                          ClientInfoStatu = b.ClientInfoStatu,
                          CountryCity = b.CountryCity,
                          CountryName = b.CountryName,
                          FirmLabel = b.FirmLabel,
                          IndustryId = b.IndustryId,
                          IndustryName = a.IndustryName,
                       };
            var count = linq.Count();
            var page = (int)Math.Ceiling(count * 1.0 / request.PageSize);
            var models = linq.OrderByDescending(x => x.IndustryId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            APIPaing<ClientInfoDTO> paging = new APIPaing<ClientInfoDTO>();
            paging.PageCount = page;
            paging.TotalCount = count;
            paging.Items = mapper.Map<List<ClientInfoDTO>>(models);
            result.data = paging;
            return Task.FromResult(result);
        }
    }
}
